package com.feihong.txgw.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RatingBar;

import com.bumptech.glide.Glide;
import com.feihong.txgw.R;
import com.feihong.txgw.adapter.Holder.BaseViewHolder;
import com.feihong.txgw.adapter.Holder.ReviewOrderFootViewHolder;
import com.feihong.txgw.adapter.Holder.ReviewOrderGoodsViewHolder;
import com.feihong.txgw.inter.listener.order.OnReviewAnoymityListener;
import com.feihong.txgw.inter.listener.order.OnReviewDestribeSelectListener;
import com.feihong.txgw.inter.listener.order.OnReviewEidtChangeListener;
import com.feihong.txgw.inter.listener.order.OnReviewFlowSelectListener;
import com.feihong.txgw.inter.listener.order.OnReviewServiceSelectListener;
import com.feihong.txgw.json.user.ProductOrderDetailEntity;
import com.ratingbar.view.MyRatingBarView;

import java.util.ArrayList;
import java.util.List;

/**
 * @name txgw_app
 * @class name：com.feihong.txgw.adapter
 * @class describe:
 * @author wangziheng
 * @time 2017/8/16 19:08
 * @change
 * @chang time
 * @class describe
 */

public class ReViewOrderAdapter extends RecyclerView.Adapter<BaseViewHolder> {
    private Context mContext;
    private Drawable mAgree,mUnAgree;
    private List<ProductOrderDetailEntity> mReviewOrderData;
    private static final int REVIEW_ORDER_GOODS_TYPE = 1;
    private static final int REVIEW_ORDER_FOOT_TYPE = 2;

    private boolean isAgree = false; // 是否匿名评论
    public ReViewOrderAdapter(Context context){
        mContext = context;
        mReviewOrderData = new ArrayList<>();

        mAgree = mContext.getResources().getDrawable(R.mipmap.review_agree);
        mUnAgree = mContext.getResources().getDrawable(R.mipmap.review_un_agree);
    }
    public void setData(List<ProductOrderDetailEntity> productOrderDetailEntities){
        mReviewOrderData = productOrderDetailEntities;
        notifyDataSetChanged();
    }
    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(REVIEW_ORDER_GOODS_TYPE == viewType){
            return new ReviewOrderGoodsViewHolder(mContext, LayoutInflater.from(mContext).inflate(R.layout.review_order_good_layout,parent,false));
        }else {
            return new ReviewOrderFootViewHolder(LayoutInflater.from(mContext).inflate(R.layout.review_order_foot_layout,parent,false));
        }
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, final int position) {
        if(REVIEW_ORDER_GOODS_TYPE == getItemViewType(position)){
                final ReviewOrderGoodsViewHolder mHolder = (ReviewOrderGoodsViewHolder) holder;
            Glide.with(mContext).load(mReviewOrderData.get(position).getSku_thumb()).into(mHolder.mGoodsImg);
            mHolder.mReviewEt.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void afterTextChanged(Editable editable) {
                        mOnReviewEditChangeListener.setOnReviewEditChangetText(position,editable.toString());
                }
            });

            mHolder.mDestribeRatingBar.setOnRatingChangeListener(new MyRatingBarView.OnRatingChangeListener() {
                @Override
                public void onRatingChanged(MyRatingBarView ratingBar, float rating) {
                    mOnReviewDestribeSelectListener.setOnReviewDestribeSelectListtener(position,new Float(rating).intValue());
                }
            });

            mHolder.mReviewAnoymityImg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(isAgree){
                        mHolder.mReviewAnoymityImg.setImageDrawable(mUnAgree);
                    }else {
                        mHolder.mReviewAnoymityImg.setImageDrawable(mAgree);
                    }
                    isAgree = !isAgree;
                    mOnAnyomityListener.setOnAnoymitySelect(position,isAgree);
                }
            });

        }else {
            final ReviewOrderFootViewHolder mHolder = (ReviewOrderFootViewHolder) holder;
            mHolder.mFlowRatingBar.setOnRatingChangeListener(new MyRatingBarView.OnRatingChangeListener() {
                @Override
                public void onRatingChanged(MyRatingBarView ratingBar, float rating) {
                    mOnReviewFlowSelectListener.setOnReviewFlowSelectListener(new Float(rating).intValue());
                }
            });

            mHolder.mServiceRatingBar.setOnRatingChangeListener(new MyRatingBarView.OnRatingChangeListener() {
                @Override
                public void onRatingChanged(MyRatingBarView ratingBar, float rating) {
                    mOnReviewServiceSelectListener.setOnReviewSerivceSelectListener(new Float(rating).intValue());
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mReviewOrderData.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if(position < mReviewOrderData.size()){
            return REVIEW_ORDER_GOODS_TYPE;
        }else {
            return REVIEW_ORDER_FOOT_TYPE;
        }
    }

    private OnReviewAnoymityListener mOnAnyomityListener;
    private OnReviewFlowSelectListener mOnReviewFlowSelectListener;
    private OnReviewDestribeSelectListener mOnReviewDestribeSelectListener;
    private OnReviewServiceSelectListener mOnReviewServiceSelectListener;
    private OnReviewEidtChangeListener mOnReviewEditChangeListener;

    public void setOnAnyomityListener(OnReviewAnoymityListener onAnyomityListener){
        mOnAnyomityListener = onAnyomityListener;
    }

    public void setOnReviewEditChangeLisetener(OnReviewEidtChangeListener onReviewEditChangeLisetener){
        mOnReviewEditChangeListener = onReviewEditChangeLisetener;
    }

    public void setOnReviewDestribeSelectListener(OnReviewDestribeSelectListener onReviewDestribeSelectListener){
        mOnReviewDestribeSelectListener = onReviewDestribeSelectListener;
    }

    public void setOnFlowSelectListener(OnReviewFlowSelectListener onFlowSelectListener){
        mOnReviewFlowSelectListener = onFlowSelectListener;
    }

    public void setOnServiceSelectListener(OnReviewServiceSelectListener onServiceSelectListener){
        mOnReviewServiceSelectListener = onServiceSelectListener;
    }
}
